package com.ysd.lis.service.synItemData.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysExtenFieldsValue;
import com.ysd.lis.entity.lab.LabItemDict;
import com.ysd.lis.entity.synItemData.LabBaseItem;
import com.ysd.lis.mapper.lab.LabItemDictMapper;
import com.ysd.lis.mapper.synItemData.LabBaseItemMapper;
import com.ysd.lis.mapper.sys.SysExtenFieldsValueMapper;
import com.ysd.lis.service.SysExtenFieldsValueService;
import com.ysd.lis.service.synItemData.LabBaseItemService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wh
 * @since 2025-05-16
 */
@Service
public class LabBaseItemServiceImpl extends ServiceImpl<LabBaseItemMapper, LabBaseItem> implements LabBaseItemService {

    @Autowired
    private LabBaseItemMapper labBaseItemMapper;
    @Autowired
    private LabItemDictMapper labItemDictMapper;

    @Autowired
    SysExtenFieldsValueService sysExtenFieldsValueService;

    private static final Logger logger = LoggerFactory.getLogger(LabBaseItemServiceImpl.class);

    @Override
    public Result synOldLisdItemDict() {
        List<LabItemDict> itemDicts = labItemDictMapper.selectList(new LambdaQueryWrapper<LabItemDict>().eq(LabItemDict::getDelFlag, 0));
        Map<String, List<LabItemDict>> itemDictMap = itemDicts.stream().filter(p -> StringUtils.isNotBlank(p.getItemNo())).collect(Collectors.groupingBy(LabItemDict::getItemNo));
        List<LabBaseItem> oldItems = labBaseItemMapper.selectList(new LambdaQueryWrapper<LabBaseItem>().eq(LabBaseItem::getDelFlag, 0));
        logger.info("开始执行lis报告项目知识库同步:" );
        int count = 0;
        List<String> itemList=new ArrayList<>();
        for (LabBaseItem oldItem : oldItems) {
            if (StringUtils.isNotBlank(oldItem.getNameEn())){
                List<LabItemDict> itemDicts1 = itemDictMap.get(oldItem.getNameEn());
                if (CollectionUtil.isNotEmpty(itemDicts1)){
                    for (LabItemDict labItemDict : itemDicts1) {
                        SysExtenFieldsValue operFieldsValue = new SysExtenFieldsValue();
                        operFieldsValue.setFieldId("1787668556865028098");
                        operFieldsValue.setFieldName("操作说明");
                        operFieldsValue.setFieldCode("operationInstruction");
                        operFieldsValue.setBusRowId(labItemDict.getId());
                        operFieldsValue.setDataType("Strng");
                        operFieldsValue.setValue(oldItem.getNotes());
                        sysExtenFieldsValueService.saveOrUpdate(operFieldsValue,new LambdaQueryWrapper<SysExtenFieldsValue>().eq(SysExtenFieldsValue::getBusRowId, labItemDict.getId()).eq(SysExtenFieldsValue::getFieldId, "1787668556865028098").eq(SysExtenFieldsValue::getDelFlag, 0));

                        SysExtenFieldsValue clinFieldsValue = new SysExtenFieldsValue();
                        clinFieldsValue.setFieldId("1787668556890193921");
                        clinFieldsValue.setFieldName("临床意义");
                        clinFieldsValue.setFieldCode("clinicalSignificance");
                        clinFieldsValue.setBusRowId(labItemDict.getId());
                        clinFieldsValue.setDataType("Strng");
                        clinFieldsValue.setValue(oldItem.getClinicalsignificance());
                        sysExtenFieldsValueService.saveOrUpdate(clinFieldsValue,new LambdaQueryWrapper<SysExtenFieldsValue>().eq(SysExtenFieldsValue::getBusRowId, labItemDict.getId()).eq(SysExtenFieldsValue::getFieldId, "1787668556890193921").eq(SysExtenFieldsValue::getDelFlag, 0));
                        itemList.add(labItemDict.getItemNa());
                        count++;
                    }
                }

            }
        }
        logger.info("lis报告项目知识库同步完成,同步数量为:" + count);
        List<String> itemes = itemList.stream().distinct().collect(Collectors.toList());
        logger.info("同步报告项目:" + itemes);
        return null;
    }
}
